package org.example.service.impl;

import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.domain.R;
import org.example.domain.Setmeal;
import org.example.domain.SetmealDish;
import org.example.dto.SetmealDto;
import org.example.mapper.SetmealMapper;
import org.example.service.SetmealDishService;
import org.example.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    @Transactional
    public void saveWithSetmealDish(SetmealDto setmealDto) {
        this.save(setmealDto);
        Long id = setmealDto.getId();
        for (SetmealDish setmealDish :
                setmealDto.getSetmealDishes()) {
            setmealDish.setSetmealId(id);
        }
        setmealDishService.saveBatch(setmealDto.getSetmealDishes());
    }

    @Override
    public R<Page<SetmealDto>> getWithSetmealDishName(int page, int pageSize, String name) {
        List<SetmealDto> withSetmealDishName = setmealMapper.getWithSetmealDishName();
        Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);
        setmealDtoPage.setRecords(withSetmealDishName);
        return R.success(setmealDtoPage);
    }

    @Override
    @Transactional

    public R<String> delWithDish(@RequestParam List<Long> ids) {
//        for (Long id :
//                ids) {
//            Setmeal setmeal = this.getById(id);
//            Integer status = setmeal.getStatus();
//            if (status==0){
//                return R.error("error");
//            }
//        }
//        for (Long id :
//                ids) {
//            LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
//            setmealDishService.remove(setmealDishLambdaQueryWrapper);
//        }
//        for (Long id :
//                ids) {
//            this.removeById(id);
//        }
//
//        return R.success("success");
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Setmeal::getId, ids);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus, 1);
        int count = this.count(setmealLambdaQueryWrapper);
        for (Long id :
                ids) {
            Setmeal setmeal = this.getById(id);
            String image = setmeal.getImage();
            HashMap<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("body", image);
            String post = HttpUtil.get("http://localhost:8080/gitee/del", objectObjectHashMap);


        }
        if (count > 0) {
            return R.error("error");
        }
        this.removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
        return R.success("删除成功");
    }

}
